package com.gk.javaservice.controller;

import com.alibaba.fastjson.JSONObject;
import com.gk.javaservice.common.ServerResponse;
import com.gk.javaservice.util.DownLoadManager.FileDownloader;
import com.gk.javaservice.util.serialTool.*;
import gnu.io.SerialPort;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/Test")
public class TestController {
    private Logger log = Logger.getLogger(this.getClass());

    @CrossOrigin
    @ResponseBody
    @RequestMapping("/SendTest1")
    public ServerResponse<?> SendTest1(HttpServletRequest request) {
        // System.out.println("in to SendTest1");
        String msg = request.getParameter("msg");
        String port = request.getParameter("port");
        String fat = request.getParameter("fat");
        int bau = Integer.parseInt(request.getParameter("bau"));
        String res = "";

        SerialPort serialPort = null;                  //打开的端
        try {
            //打开串口
            serialPort = SerialTool.openPort(port, bau);
            byte[] bs;
            if (fat.equals("HEX")) {
                bs = hex2byte(msg);
            } else {
                msg = convertStringToHex(msg);
                bs = hex2byte(msg);
            }
            //写入数组
            SerialTool.sendToPort(serialPort, bs);
        } catch (SendDataToSerialPortFailure sendDataToSerialPortFailure) {
            sendDataToSerialPortFailure.printStackTrace();
        } catch (PortInUse portInUse) {
            portInUse.printStackTrace();
        } catch (SerialPortOutputStreamCloseFailure serialPortOutputStreamCloseFailure) {
            serialPortOutputStreamCloseFailure.printStackTrace();
        } catch (NoSuchPort noSuchPort) {
            noSuchPort.printStackTrace();
        } catch (NotASerialPort notASerialPort) {
            notASerialPort.printStackTrace();
        } catch (SerialPortParameterFailure serialPortParameterFailure) {
            serialPortParameterFailure.printStackTrace();
        } finally {
            //关闭串口
            SerialTool.closePort(serialPort);
        }


        return ServerResponse.createBySuccess(res);
    }

    public static byte[] hex2byte(String hex) {
        String digital = "0123456789ABCDEF";
        String hex1 = hex.replace(" ", "");
        char[] hex2char = hex1.toCharArray();
        byte[] bytes = new byte[hex1.length() / 2];
        byte temp;
        for (int p = 0; p < bytes.length; p++) {
            temp = (byte) (digital.indexOf(hex2char[2 * p]) * 16);
            temp += digital.indexOf(hex2char[2 * p + 1]);
            bytes[p] = (byte) (temp & 0xff);
        }
        return bytes;
    }

    public String convertStringToHex(String str) {

        char[] chars = str.toCharArray();
        String MSG = "";
        for (int i = 0; i < chars.length; i++) {
            MSG += " " + Integer.toHexString((int) chars[i]);
        }
        MSG = MSG.trim();
        MSG += " 0d 0a";
        MSG = MSG.toUpperCase();
        return MSG;
    }


    @CrossOrigin
    @ResponseBody
    @RequestMapping("/httpSaveFileDown")
    public ServerResponse<?> httpSaveFileDown(@RequestBody JSONObject para) {
        log.info("in to httpSaveFileDown:");

        String url = para.getString("url");
        String method = para.getString("method");
        FileDownloader hc = new FileDownloader();
        return ServerResponse.createBySuccess(
                hc.saveFileDownNoSSL(url,
                        "E://server/datas\\GK480490/",
                        "902562392.mp4", method));

    }
}
